package cn.hinglo.website.service;

import cn.hinglo.common.utils.SysCommonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @createTime: 2018/9/26 17:50
 * @author: HingLo
 * @description: 菜单管理的服务层
 */
@Service
public class MenuService {

    /**
     * 菜单缓存名称
     */
    private final static String CACHE_NAME = "menu";

    /**
     * 请求的菜单列表
     *
     * @return 返回结果
     */
    public List<Map<String, Object>> listService() {
        // 先从菜单中取值
        List<Record> list = CacheKit.get(CACHE_NAME, "list");
        // 如果没有就从数据库中查询
        if (list == null || list.isEmpty()) {
            String sql = Db.getSql("website.menu-list");
            list = SysCommonKit.createTreeMenuKit(Db.find(sql));
            if (list != null && !list.isEmpty()) {
                CacheKit.put(CACHE_NAME, "list", list);
            }
        }
        return SysCommonKit.recordToMapKit(list);
    }
}
